<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>JavaScript 常用技巧 | 时间领主的博客</title>
    <meta name="generator" content="VuePress 1.4.1">
    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/@babel/standalone/babel.min.js"></script>
    <link rel="icon" href="/blog/favicon.ico">
    <meta name="description" content="行动不一定带来快乐，而无行动则决无快乐">
    <meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=no">
    <link rel="preload" href="/blog/assets/css/0.styles.174d23e9.css" as="style"><link rel="preload" href="/blog/assets/js/app.c6f0b635.js" as="script"><link rel="preload" href="/blog/assets/js/3.56d0db2f.js" as="script"><link rel="preload" href="/blog/assets/js/1.dc177a2a.js" as="script"><link rel="preload" href="/blog/assets/js/15.e6ebcc21.js" as="script"><link rel="prefetch" href="/blog/assets/js/10.3ea1e65e.js"><link rel="prefetch" href="/blog/assets/js/11.b138a709.js"><link rel="prefetch" href="/blog/assets/js/12.98d3dec7.js"><link rel="prefetch" href="/blog/assets/js/13.1a8500fb.js"><link rel="prefetch" href="/blog/assets/js/14.d52be3d5.js"><link rel="prefetch" href="/blog/assets/js/16.515c742f.js"><link rel="prefetch" href="/blog/assets/js/17.8ebf8e60.js"><link rel="prefetch" href="/blog/assets/js/18.b7c7c6f0.js"><link rel="prefetch" href="/blog/assets/js/19.0ccdcb93.js"><link rel="prefetch" href="/blog/assets/js/20.78e9f5a5.js"><link rel="prefetch" href="/blog/assets/js/21.5e84bd81.js"><link rel="prefetch" href="/blog/assets/js/22.eff4bf70.js"><link rel="prefetch" href="/blog/assets/js/23.1417d381.js"><link rel="prefetch" href="/blog/assets/js/24.013e4313.js"><link rel="prefetch" href="/blog/assets/js/25.f3067eb6.js"><link rel="prefetch" href="/blog/assets/js/26.19fe9c6d.js"><link rel="prefetch" href="/blog/assets/js/27.c089c5d4.js"><link rel="prefetch" href="/blog/assets/js/28.6ff3368c.js"><link rel="prefetch" href="/blog/assets/js/29.d1cc7d1f.js"><link rel="prefetch" href="/blog/assets/js/30.0dc56898.js"><link rel="prefetch" href="/blog/assets/js/31.b5bac1d5.js"><link rel="prefetch" href="/blog/assets/js/32.fbc30039.js"><link rel="prefetch" href="/blog/assets/js/33.7070e81d.js"><link rel="prefetch" href="/blog/assets/js/34.e7c86927.js"><link rel="prefetch" href="/blog/assets/js/35.c731e557.js"><link rel="prefetch" href="/blog/assets/js/36.151c6972.js"><link rel="prefetch" href="/blog/assets/js/37.019d3b29.js"><link rel="prefetch" href="/blog/assets/js/4.6a4255d5.js"><link rel="prefetch" href="/blog/assets/js/5.829faed3.js"><link rel="prefetch" href="/blog/assets/js/6.b91d75cb.js"><link rel="prefetch" href="/blog/assets/js/7.7aafbc48.js"><link rel="prefetch" href="/blog/assets/js/8.982bf0b3.js"><link rel="prefetch" href="/blog/assets/js/9.283b0ce1.js">
    <link rel="stylesheet" href="/blog/assets/css/0.styles.174d23e9.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div><div class="theme-container" data-v-4698c43e><div data-v-4698c43e><div id="loader-wrapper" class="loading-wrapper" data-v-4b73742e data-v-4698c43e data-v-4698c43e><div class="loader-main" data-v-4b73742e><div data-v-4b73742e></div><div data-v-4b73742e></div><div data-v-4b73742e></div><div data-v-4b73742e></div></div> <!----> <!----></div> <div class="password-shadow password-wrapper-out" style="display:none;" data-v-6cbeab0a data-v-4698c43e data-v-4698c43e><h3 class="title" style="display:none;" data-v-6cbeab0a data-v-6cbeab0a>时间领主的博客</h3> <!----> <label id="box" class="inputBox" style="display:none;" data-v-6cbeab0a data-v-6cbeab0a><input type="password" value="" data-v-6cbeab0a> <span data-v-6cbeab0a>Konck! Knock!</span> <button data-v-6cbeab0a>OK</button></label> <div class="footer" style="display:none;" data-v-6cbeab0a data-v-6cbeab0a><span data-v-6cbeab0a><i class="iconfont reco-theme" data-v-6cbeab0a></i> <a target="blank" href="https://vuepress-theme-reco.recoluan.com" data-v-6cbeab0a>vuePress-theme-reco</a></span> <span data-v-6cbeab0a><i class="iconfont reco-copyright" data-v-6cbeab0a></i> <a data-v-6cbeab0a><span data-v-6cbeab0a>时间领主</span>
            
          <span data-v-6cbeab0a>2019 - </span>
          2020
        </a></span></div></div> <div class="hide" data-v-4698c43e><header class="navbar" data-v-4698c43e><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/blog/" class="home-link router-link-active"><!----> <span class="site-name">时间领主的博客</span></a> <div class="links"><div class="color-picker"><a class="color-button"><i class="iconfont reco-color"></i></a> <div class="color-picker-menu" style="display:none;"><div class="mode-options"><h4 class="title">Choose mode</h4> <ul class="color-mode-options"><li class="dark">dark</li><li class="auto active">auto</li><li class="light">light</li></ul></div></div></div> <div class="search-box"><i class="iconfont reco-search"></i> <input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/blog/" class="nav-link"><i class="iconfont reco-home"></i>
  首页
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-category"></i>
      分类
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/blog/categories/前端/" class="nav-link"><i class="iconfont undefined"></i>
  前端
</a></li><li class="dropdown-item"><!----> <a href="/blog/categories/Light-UI 组件库/" class="nav-link"><i class="iconfont undefined"></i>
  Light-UI 组件库
</a></li><li class="dropdown-item"><!----> <a href="/blog/categories/数据库/" class="nav-link"><i class="iconfont undefined"></i>
  数据库
</a></li><li class="dropdown-item"><!----> <a href="/blog/categories/问题/" class="nav-link"><i class="iconfont undefined"></i>
  问题
</a></li><li class="dropdown-item"><!----> <a href="/blog/categories/面试/" class="nav-link"><i class="iconfont undefined"></i>
  面试
</a></li></ul></div></div><div class="nav-item"><a href="/blog/tag/" class="nav-link"><i class="iconfont reco-tag"></i>
  标签
</a></div><div class="nav-item"><a href="https://gitee.com/caohuiboss" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont undefined"></i>
  码云
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></div><div class="nav-item"><a href="https://cn.bing.com/" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont undefined"></i>
  必应
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></div> <!----></nav></div></header> <div class="sidebar-mask" data-v-4698c43e></div> <aside class="sidebar" data-v-4698c43e><div class="personal-info-wrapper" data-v-6c8ffc9c><img src="/blog/avatar.gif" alt="author-avatar" class="personal-img" data-v-6c8ffc9c> <h3 class="name" data-v-6c8ffc9c>
    时间领主
  </h3> <div class="num" data-v-6c8ffc9c><div data-v-6c8ffc9c><h3 data-v-6c8ffc9c>26</h3> <h6 data-v-6c8ffc9c>文章</h6></div> <div data-v-6c8ffc9c><h3 data-v-6c8ffc9c>8</h3> <h6 data-v-6c8ffc9c>标签</h6></div></div> <hr data-v-6c8ffc9c></div> <nav class="nav-links"><div class="nav-item"><a href="/blog/" class="nav-link"><i class="iconfont reco-home"></i>
  首页
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-category"></i>
      分类
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/blog/categories/前端/" class="nav-link"><i class="iconfont undefined"></i>
  前端
</a></li><li class="dropdown-item"><!----> <a href="/blog/categories/Light-UI 组件库/" class="nav-link"><i class="iconfont undefined"></i>
  Light-UI 组件库
</a></li><li class="dropdown-item"><!----> <a href="/blog/categories/数据库/" class="nav-link"><i class="iconfont undefined"></i>
  数据库
</a></li><li class="dropdown-item"><!----> <a href="/blog/categories/问题/" class="nav-link"><i class="iconfont undefined"></i>
  问题
</a></li><li class="dropdown-item"><!----> <a href="/blog/categories/面试/" class="nav-link"><i class="iconfont undefined"></i>
  面试
</a></li></ul></div></div><div class="nav-item"><a href="/blog/tag/" class="nav-link"><i class="iconfont reco-tag"></i>
  标签
</a></div><div class="nav-item"><a href="https://gitee.com/caohuiboss" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont undefined"></i>
  码云
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></div><div class="nav-item"><a href="https://cn.bing.com/" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont undefined"></i>
  必应
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></div> <!----></nav>  <ul class="sidebar-links"><li><section class="sidebar-group depth-0"><p class="sidebar-heading open"><span>JavaScript 常用技巧</span> <!----></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/blog/views/JavaScript/JavaScript%20%E5%B8%B8%E7%94%A8%E6%8A%80%E5%B7%A7.html#_1-过滤唯一值" class="sidebar-link">1.过滤唯一值</a><ul class="sidebar-sub-headers"></ul></li><li><a href="/blog/views/JavaScript/JavaScript%20%E5%B8%B8%E7%94%A8%E6%8A%80%E5%B7%A7.html#_2-与-或-的运算" class="sidebar-link">2.与 或  的运算</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/blog/views/JavaScript/JavaScript%20%E5%B8%B8%E7%94%A8%E6%8A%80%E5%B7%A7.html#它是怎么工作的" class="sidebar-link">它是怎么工作的</a></li></ul></li><li><a href="/blog/views/JavaScript/JavaScript%20%E5%B8%B8%E7%94%A8%E6%8A%80%E5%B7%A7.html#_3-转换为布尔值" class="sidebar-link">3.转换为布尔值</a><ul class="sidebar-sub-headers"></ul></li><li><a href="/blog/views/JavaScript/JavaScript%20%E5%B8%B8%E7%94%A8%E6%8A%80%E5%B7%A7.html#_4-转化为字符串" class="sidebar-link">4.转化为字符串</a><ul class="sidebar-sub-headers"></ul></li><li><a href="/blog/views/JavaScript/JavaScript%20%E5%B8%B8%E7%94%A8%E6%8A%80%E5%B7%A7.html#_5-转化为数字" class="sidebar-link">5.转化为数字</a><ul class="sidebar-sub-headers"></ul></li><li><a href="/blog/views/JavaScript/JavaScript%20%E5%B8%B8%E7%94%A8%E6%8A%80%E5%B7%A7.html#_6-性能更好的运算" class="sidebar-link">6. 性能更好的运算</a><ul class="sidebar-sub-headers"></ul></li><li><a href="/blog/views/JavaScript/JavaScript%20%E5%B8%B8%E7%94%A8%E6%8A%80%E5%B7%A7.html#_7-快速浮点数转整数" class="sidebar-link">7.快速浮点数转整数</a><ul class="sidebar-sub-headers"></ul></li><li><a href="/blog/views/JavaScript/JavaScript%20%E5%B8%B8%E7%94%A8%E6%8A%80%E5%B7%A7.html#_8-类中的自动绑定" class="sidebar-link">8. 类中的自动绑定</a><ul class="sidebar-sub-headers"></ul></li><li><a href="/blog/views/JavaScript/JavaScript%20%E5%B8%B8%E7%94%A8%E6%8A%80%E5%B7%A7.html#_9-数组截断" class="sidebar-link">9. 数组截断</a><ul class="sidebar-sub-headers"></ul></li><li><a href="/blog/views/JavaScript/JavaScript%20%E5%B8%B8%E7%94%A8%E6%8A%80%E5%B7%A7.html#_10-获取数组中的最后一项" class="sidebar-link">10. 获取数组中的最后一项</a><ul class="sidebar-sub-headers"></ul></li><li><a href="/blog/views/JavaScript/JavaScript%20%E5%B8%B8%E7%94%A8%E6%8A%80%E5%B7%A7.html#_11-格式化-json-代码" class="sidebar-link">11.格式化 JSON 代码</a><ul class="sidebar-sub-headers"></ul></li></ul></section></li></ul> </aside> <div class="password-shadow password-wrapper-in" style="display:none;" data-v-6cbeab0a data-v-4698c43e><h3 class="title" style="display:none;" data-v-6cbeab0a data-v-6cbeab0a>JavaScript 常用技巧</h3> <!----> <label id="box" class="inputBox" style="display:none;" data-v-6cbeab0a data-v-6cbeab0a><input type="password" value="" data-v-6cbeab0a> <span data-v-6cbeab0a>Konck! Knock!</span> <button data-v-6cbeab0a>OK</button></label> <div class="footer" style="display:none;" data-v-6cbeab0a data-v-6cbeab0a><span data-v-6cbeab0a><i class="iconfont reco-theme" data-v-6cbeab0a></i> <a target="blank" href="https://vuepress-theme-reco.recoluan.com" data-v-6cbeab0a>vuePress-theme-reco</a></span> <span data-v-6cbeab0a><i class="iconfont reco-copyright" data-v-6cbeab0a></i> <a data-v-6cbeab0a><span data-v-6cbeab0a>时间领主</span>
            
          <span data-v-6cbeab0a>2019 - </span>
          2020
        </a></span></div></div> <div data-v-4698c43e><main class="page"><!----> <div class="page-title" style="display:none;"><h1>JavaScript 常用技巧</h1> <hr> <div data-v-484a899e><i class="iconfont reco-account" data-v-484a899e><span data-v-484a899e>时间领主</span></i> <i class="iconfont reco-date" data-v-484a899e><span data-v-484a899e>2020-03-01</span></i> <!----> <i class="iconfont reco-tag tags" data-v-484a899e><span class="tag-item" data-v-484a899e>
      JavaScript
    </span></i></div></div> <div class="theme-reco-content content__default" style="display:none;"><h1 id="javascript-常用技巧"><a href="#javascript-常用技巧" class="header-anchor">#</a> JavaScript 常用技巧</h1> <p>我将分享一些我认为特别有用的技巧，后续会持续补充。来源-掘金知乎等</p> <h2 id="_1-过滤唯一值"><a href="#_1-过滤唯一值" class="header-anchor">#</a> 1.过滤唯一值</h2> <p>Set对象类型是在 ES6 中引入的，配合展开操作...一起，我们可以使用它来创建一个新数组，该数组只有唯一的值。</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>const array = [1, 1, 2, 3, 5, 5, 1];
const uniqueArray = [...new Set(array)];
console.log(uniqueArray); // Result: [1, 2, 3, 5]

</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><p>在 ES6 之前，隔离唯一值将涉及比这多得多的代码。</p> <p>此技巧适用于包含基本类型的数组：undefined，null，boolean，string和number。 （如果有一个包含对象，函数或其他数组的数组，此方法不适用）</p> <h2 id="_2-与-或-的运算"><a href="#_2-与-或-的运算" class="header-anchor">#</a> 2.与 或  的运算</h2> <p>三元运算符是编写简单（有时不那么简单）条件语句的快速方法，如下所示：</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>x &gt; 100 ? &quot;Above 100&quot; : &quot;Below 100&quot;;
x &gt; 100 ? (x &gt; 200 ? &quot;Above 200&quot; : &quot;Between 100-200&quot;) : &quot;Below 100&quot;;

</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p>但有时使用三元运算符处理也会很复杂。 相反，我们可以使用’与’&amp;&amp;和’或’|| 逻辑运算符以更简洁的方式书写表达式。 这通常被称为“短路”或“短路运算”。</p> <h3 id="它是怎么工作的"><a href="#它是怎么工作的" class="header-anchor">#</a> 它是怎么工作的</h3> <p>假设我们只想返回两个或多个选项中的一个。
使用&amp;&amp;将返回第一个条件为假的值。如果每个操作数的计算值都为true，则返回最后一个计算过的表达式</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>let one = 1,
    two = 2,
    three = 3;
console.log(one &amp;&amp; two &amp;&amp; three); // Result: 3
console.log(0 &amp;&amp; null); // Result: 0
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><p>使用||将返回第一个条件为真的值。如果每个操作数的计算结果都为false，则返回最后一个计算过的表达式。</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>let one = 1,
    two = 2,
    three = 3;
console.log(one || two || three); // Result: 1
console.log(0 || null); // Result: null

</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><h4 id="例一"><a href="#例一" class="header-anchor">#</a> 例一</h4> <p>假设我们想返回一个变量的长度，但是我们不知道变量的类型。</p> <p>我们可以使用if/else语句来检查foo是可接受的类型，但是这可能会变得非常冗长。或运行可以帮助我们简化操作：</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>return (foo || []).length;
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>如果变量foo是 true，它将被返回。否则，将返回空数组的长度:0。</p> <h4 id="例二"><a href="#例二" class="header-anchor">#</a> 例二</h4> <p>你是否遇到过访问嵌套对象属性的问题？ 你可能不知道对象或其中一个子属性是否存在，这可能会导致令人沮丧的错误。</p> <p>假设我们想在this.state中访问一个名为data的属性，但是在我们的程序成功返回一个获取请求之前，data 是未定义的。</p> <p>根据我们使用它的位置，调用this.state.data可能会阻止我们的应用程序运行。 为了解决这个问题，我们可以将其做进一步的判断：</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>if (this.state.data) {
    return this.state.data;
} else {
    return &quot;Fetching Data&quot;;
}
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><p>但这似乎很重复。 ‘或' 运算符提供了更简洁的解决方案：</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>return this.state.data || &quot;Fetching Data&quot;;

</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><h2 id="_3-转换为布尔值"><a href="#_3-转换为布尔值" class="header-anchor">#</a> 3.转换为布尔值</h2> <p>除了常规的布尔值true和false之外，JavaScript 还将所有其他值视为 ‘true’ 或‘false’。</p> <p>除非另有定义，否则 JavaScript 中的所有值都是’true’，除了 0，“”，null，undefined，NaN，当然还有false，这些都是‘false’</p> <p>可以通过使用负算运算符轻松地在true和false之间切换。它也会将类型转换为“boolean”。</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>const isTrue = !0;
const isFalse = !1;
const alsoFalse = !!0;
console.log(isTrue); // Result: true
console.log(typeof true); // Result: &quot;boolean&quot;

</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><h2 id="_4-转化为字符串"><a href="#_4-转化为字符串" class="header-anchor">#</a> 4.转化为字符串</h2> <p>要快速地将数字转换为字符串，我们可以使用连接运算符+后跟一组空引号&quot;&quot;。</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>const val = 1 + &quot;&quot;;
console.log(val); // Result: &quot;1&quot;
console.log(typeof val); // Result: &quot;string&quot;

</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><h2 id="_5-转化为数字"><a href="#_5-转化为数字" class="header-anchor">#</a> 5.转化为数字</h2> <p>使用加法运算符+可以快速实现相反的效果。</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>let int = &quot;15&quot;;
int = +int;
console.log(int); // Result: 15
console.log(typeof int);
Result: &quot;number&quot;;
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><p>这也可以用于将布尔值转换为数字，如下所示</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>console.log(+true); // Return: 1
console.log(+false); // Return: 0
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p>在某些上下文中，+将被解释为连接操作符，而不是加法操作符。当这种情况发生时(你希望返回一个整数，而不是浮点数)，您可以使用两个波浪号: ~~ 。
连续使用两个波浪有效地否定了操作，因为— ( — n — 1) — 1 = n + 1 — 1 = n。 换句话说，~—16 等于15。</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>const int = ~~&quot;15&quot;;
console.log(int); // Result: 15
console.log(typeof int);
Result: &quot;number&quot;;
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><h2 id="_6-性能更好的运算"><a href="#_6-性能更好的运算" class="header-anchor">#</a> 6. 性能更好的运算</h2> <p>从 ES7 开始，可以使用指数运算符 ** 作为幂的简写，这比编写Math.pow(2, 3) 更快。 这是很简单的东西，但它之所以出现在列表中，是因为没有多少教程更新过这个操作符。</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>console.log(2 ** 3); // Result: 8
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>这不应该与通常用于表示指数的^符号相混淆，但在 JavaScript 中它是按位异或运算符。</p> <p>在 ES7 之前，只有以2为基数的幂才存在简写，使用按位左移操作符&lt;&lt;</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>Math.pow(2, n);
2 &lt;&lt; (n - 1);
2 ** n;
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p>例如，2 &lt;&lt; 3 = 16等于2 ** 4 = 16。</p> <h2 id="_7-快速浮点数转整数"><a href="#_7-快速浮点数转整数" class="header-anchor">#</a> 7.快速浮点数转整数</h2> <p>如果希望将浮点数转换为整数，可以使用Math.floor()、Math.ceil()或Math.round()。但是还有一种更快的方法可以使用|(位或运算符)将浮点数截断为整数。</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>console.log(23.9 | 0); // Result: 23
console.log(-23.9 | 0); // Result: -23
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p>|的行为取决于处理的是正数还是负数，所以最好只在确定的情况下使用这个快捷方式。</p> <p>如果n为正，则n | 0有效地向下舍入。 如果n为负数，则有效地向上舍入。 更准确地说，此操作将删除小数点后面的任何内容，将浮点数截断为整数。</p> <p>你可以使用 ~~ 来获得相同的舍入效果，如上所述，实际上任何位操作符都会强制浮点数为整数。这些特殊操作之所以有效，是因为一旦强制为整数，值就保持不变。</p> <h4 id="删除最后一个数字"><a href="#删除最后一个数字" class="header-anchor">#</a> 删除最后一个数字</h4> <p>按位或运算符还可以用于从整数的末尾删除任意数量的数字。这意味着我们不需要使用这样的代码来在类型之间进行转换。</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>let str = &quot;1553&quot;;
Number(str.substring(0, str.length - 1));
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p>相反，按位或运算符可以这样写：</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>console.log((1553 / 10) | 0); // Result: 155
console.log((1553 / 100) | 0); // Result: 15
console.log((1553 / 1000) | 0); // Result: 1
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><h2 id="_8-类中的自动绑定"><a href="#_8-类中的自动绑定" class="header-anchor">#</a> 8. 类中的自动绑定</h2> <p>我们可以在类方法中使用 ES6 箭头表示法，并且通过这样做可以隐含绑定。 这通常会在我们的类构造函数中保存几行代码，我们可以愉快地告别重复的表达式，例如this.myMethod = this.myMethod.bind(this)</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>import React, { Component } from React;
export default class App extends Compononent {
  constructor(props) {
  super(props);
  this.state = {};
  }
myMethod = () =&gt; {
    // react 常用写法了
    // This method is bound implicitly!
  }
render() {
    return (
      &lt;&gt;
        &lt;div&gt;
          {this.myMethod()}
        &lt;/div&gt;
      &lt;/&gt;
    )
  }
};
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br></div></div><h2 id="_9-数组截断"><a href="#_9-数组截断" class="header-anchor">#</a> 9. 数组截断</h2> <p>如果要从数组的末尾删除值，有比使用splice()更快的方法。
例如，如果你知道原始数组的大小，您可以重新定义它的length属性，就像这样</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>let array = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
array.length = 4;
console.log(array); // Result: [0, 1, 2, 3]
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p>这是一个特别简洁的解决方案。但是，我发现slice()方法的运行时更快。如果速度是你的主要目标，考虑使用：</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>let array = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
array = array.slice(0, 4);
console.log(array); // Result: [0, 1, 2, 3]
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><h2 id="_10-获取数组中的最后一项"><a href="#_10-获取数组中的最后一项" class="header-anchor">#</a> 10. 获取数组中的最后一项</h2> <p>数组方法slice()可以接受负整数，如果提供它，它将接受数组末尾的值，而不是数组开头的值。</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>let array = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
console.log(array.slice(-1)); // Result: [9]
console.log(array.slice(-2)); // Result: [8, 9]
console.log(array.slice(-3)); // Result: [7, 8, 9]
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><h2 id="_11-格式化-json-代码"><a href="#_11-格式化-json-代码" class="header-anchor">#</a> 11.格式化 JSON 代码</h2> <p>你之前可能已经使用过JSON.stringify，但是您是否意识到它还可以帮助你缩进 JSON？</p> <p>stringify()方法有两个可选参数：一个replacer函数，可用于过滤显示的 JSON 和一个空格值。</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>console.log(JSON.stringify({ alpha: &quot;A&quot;, beta: &quot;B&quot; }, null, &quot;\t&quot;));
// Result:
// '{
//     &quot;alpha&quot;: A,
//     &quot;beta&quot;: B
// }'
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div></div> <footer class="page-edit" style="display:none;"><!----> <div class="last-updated"><span class="prefix">最后更新时间: </span> <span class="time">3/14/2020, 1:51:36 PM</span></div></footer> <!----> <!----></main> <!----> <div class="comments-wrapper" data-v-4698c43e><!----></div></div></div></div></div></div><div class="global-ui"><div class="back-to-ceiling" style="right:1rem;bottom:6rem;width:2.5rem;height:2.5rem;border-radius:.25rem;line-height:2.5rem;display:none;" data-v-a81d141e data-v-a81d141e><svg t="1574745035067" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="5404" class="icon" data-v-a81d141e><path d="M526.60727968 10.90185116a27.675 27.675 0 0 0-29.21455937 0c-131.36607665 82.28402758-218.69155461 228.01873535-218.69155402 394.07834331a462.20625001 462.20625001 0 0 0 5.36959153 69.94390903c1.00431239 6.55289093-0.34802892 13.13561351-3.76865779 18.80351572-32.63518765 54.11355614-51.75690182 118.55860487-51.7569018 187.94566865a371.06718723 371.06718723 0 0 0 11.50484808 91.98906777c6.53300375 25.50556257 41.68394495 28.14064038 52.69160883 4.22606766 17.37162448-37.73630017 42.14135425-72.50938081 72.80769204-103.21549295 2.18761121 3.04276886 4.15646224 6.24463696 6.40373557 9.22774369a1871.4375 1871.4375 0 0 0 140.04691725 5.34970492 1866.36093723 1866.36093723 0 0 0 140.04691723-5.34970492c2.24727335-2.98310674 4.21612437-6.18497483 6.3937923-9.2178004 30.66633723 30.70611158 55.4360664 65.4791928 72.80769147 103.21549355 11.00766384 23.91457269 46.15860503 21.27949489 52.69160879-4.22606768a371.15156223 371.15156223 0 0 0 11.514792-91.99901164c0-69.36717486-19.13165746-133.82216804-51.75690182-187.92578088-3.42062944-5.66790279-4.76302748-12.26056868-3.76865837-18.80351632a462.20625001 462.20625001 0 0 0 5.36959269-69.943909c-0.00994388-166.08943902-87.32547796-311.81420293-218.6915546-394.09823051zM605.93803103 357.87693858a93.93749974 93.93749974 0 1 1-187.89594924 6.1e-7 93.93749974 93.93749974 0 0 1 187.89594924-6.1e-7z" p-id="5405" data-v-a81d141e></path><path d="M429.50777625 765.63860547C429.50777625 803.39355007 466.44236686 1000.39046097 512.00932183 1000.39046097c45.56695499 0 82.4922232-197.00623328 82.5015456-234.7518555 0-37.75494459-36.9345906-68.35043303-82.4922232-68.34111062-45.57627738-0.00932239-82.52019037 30.59548842-82.51086798 68.34111062z" p-id="5406" data-v-a81d141e></path></svg></div></div></div>
    <script src="/blog/assets/js/app.c6f0b635.js" defer></script><script src="/blog/assets/js/3.56d0db2f.js" defer></script><script src="/blog/assets/js/1.dc177a2a.js" defer></script><script src="/blog/assets/js/15.e6ebcc21.js" defer></script>
  </body>
</html>
